{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Mean of Array After Removing Some Elements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: trimMean"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #删除某些元素后的数组均值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>arr</code> ，请你删除最小 <code>5%</code> 的数字和最大 <code>5%</code> 的数字后，剩余数字的平均值。</p>\n",
    "\n",
    "<p>与 <strong>标准答案</strong> 误差在 <code>10<sup>-5</sup></code> 的结果都被视为正确结果。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>arr = [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3]\n",
    "<b>输出：</b>2.00000\n",
    "<b>解释：</b>删除数组中最大和最小的元素后，所有元素都等于 2，所以平均值为 2 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>arr = [6,2,7,5,1,2,0,3,10,2,5,0,5,5,0,8,7,6,8,0]\n",
    "<b>输出：</b>4.00000\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>arr = [6,0,7,0,7,5,7,8,3,4,0,7,8,1,6,8,1,1,2,4,8,1,9,5,4,3,8,5,10,8,6,6,1,0,6,10,8,2,3,4]\n",
    "<b>输出：</b>4.77778\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>arr = [9,7,8,7,7,8,4,4,6,8,8,7,6,8,8,9,2,6,0,0,1,10,8,6,3,3,5,1,10,9,0,7,10,0,10,4,1,10,6,9,3,6,0,0,2,7,0,6,7,2,9,7,7,3,0,1,6,1,10,3]\n",
    "<b>输出：</b>5.27778\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 5：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>arr = [4,8,4,10,0,7,1,3,7,8,8,3,4,1,6,2,1,1,8,0,9,8,0,3,9,10,3,10,1,10,7,3,2,1,4,9,10,7,6,4,0,8,5,1,2,1,6,2,5,0,7,10,9,10,3,7,10,5,8,5,7,6,7,6,10,9,5,10,5,5,7,2,10,7,7,8,2,0,1,1]\n",
    "<b>输出：</b>5.29167\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>20 <= arr.length <= 1000</code></li>\n",
    "\t<li><code>arr.length</code><b> </b>是 <code>20</code> 的<strong> 倍数</strong> </li>\n",
    "\t<li><code>0 <= arr[i] <= 10<sup>5</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [mean-of-array-after-removing-some-elements](https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [mean-of-array-after-removing-some-elements](https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3]', '[6,2,7,5,1,2,0,3,10,2,5,0,5,5,0,8,7,6,8,0]', '[6,0,7,0,7,5,7,8,3,4,0,7,8,1,6,8,1,1,2,4,8,1,9,5,4,3,8,5,10,8,6,6,1,0,6,10,8,2,3,4]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        length = len(arr)\n",
    "        num = int(length*0.05)\n",
    "        arr.sort()\n",
    "        return sum(arr[num:length-num])/(length-2*num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        lens=len(arr) #计算数组的长度\n",
    "        a=int(lens*0.05) #计算该数组的5%\n",
    "        arr.sort()\n",
    "        del arr[:a]\n",
    "        del arr[-a:]\n",
    "        lenss=len(arr)\n",
    "        all=0\n",
    "        for i in arr:\n",
    "           all+=i\n",
    "        return all/lenss\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr = sorted(arr)\n",
    "\n",
    "        _len = len(arr)\n",
    "        return sum(arr[int(_len*0.05): int(_len*0.95)]) / (_len * 0.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        length = len(arr)\n",
    "        num = length//20\n",
    "        arr.sort()\n",
    "        return sum(arr[num:length-num])/(length-2*num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr = sorted(arr)\n",
    "        n = len(arr)\n",
    "        return sum(arr[n //20 : 19*n//20]) / (0.9*n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr)\n",
    "        m = n*5//100\n",
    "        return sum(arr[m:-m])/(n-2*m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr_s = sorted(arr)\n",
    "        n = int(len(arr) * 0.05)\n",
    "        print(n)\n",
    "        return sum(arr_s[n: -n])/(len(arr_s[n: -n]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr)\n",
    "        return sum(arr[n // 20: -n // 20]) / (n * 0.9)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr)\n",
    "        start, end = n // 20, n - n // 20\n",
    "        return sum(arr[start:end]) / (end - start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        return sum(sorted(arr)[len(arr)//20:int(len(arr)*0.95)])/int(len(arr)*0.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr)\n",
    "        return mean(arr[ceil(n * 0.05): ceil(n * 0.95)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        return sum(arr[len(arr)//20:-len(arr)//20])/(len(arr)*0.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr):\n",
    "        arr.sort()\n",
    "        n = len(arr) * 0.05\n",
    "        res_arr = arr[int(n):-int(n)]\n",
    "        return sum(res_arr) / len(res_arr)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        x = len(arr)\n",
    "        arr.sort()\n",
    "        res = 0\n",
    "        a = int(x * 0.05)\n",
    "        arr = arr[a : x - a]\n",
    "        sum1 = sum(arr)\n",
    "        res = sum1 / (x-2*a)\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        a = arr.__len__()\n",
    "        arr1 = arr[int(a*0.05):int(a*0.95)]\n",
    "        b = sum(arr1)/arr1.__len__()\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n=len(arr)\n",
    "        return sum(arr[n//20:n-n//20])/len(arr[n//20:n-n//20])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr)\n",
    "        return sum(arr[n // 20 : -n // 20]) / (n * 0.9)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def trimMean(self, arr: List[int]) -> float:\r\n",
    "        arr.sort()\r\n",
    "        m, n = int(len(arr) * 0.05), len(arr)\r\n",
    "        count, total = n - 2 * m, 0\r\n",
    "\r\n",
    "        for i in range(m, n - m):\r\n",
    "            total += arr[i]\r\n",
    "        \r\n",
    "        return 0.0 if count <= 0 else total / count\r\n",
    "\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        l = len(arr)\n",
    "        n = int(l*0.05)\n",
    "        # arr1 = arr[n:l-n]\n",
    "        return sum(arr[n:l-n]) / (l-2*n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        n = len(arr)\n",
    "        num = n//20\n",
    "        sum_ = 0\n",
    "        arr.sort()\n",
    "        for i in range(num,n-num):\n",
    "            sum_ += arr[i]\n",
    "        return sum_/(n-2*num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = int(len(arr) * 0.05)\n",
    "        return 1.0*sum(arr[n:n*(-1)])/(len(arr)-2*n)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        l = len(arr)\n",
    "        n = l // 20\n",
    "        arr.sort()\n",
    "        return (sum(arr) - sum(arr[:n]) - sum(arr[l - n:])) / (l - n * 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        l=len(arr)\n",
    "        time=l//20\n",
    "        suma=0\n",
    "        for i in range(time,l-time):\n",
    "            suma+=arr[i]\n",
    "        return suma*10/9/l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        n=len(arr)\n",
    "        arr.sort()\n",
    "        return sum(arr[n // 20: -n // 20])/(n*0.9)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr) * 0.05\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            arr.pop(0)\n",
    "            arr.pop(-1)\n",
    "            i += 1\n",
    "        return sum(arr)/len(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        n = len(arr)\n",
    "        arr.sort()\n",
    "        return sum(arr[n // 20: n - n // 20]) / (n - n // 10)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, a: List[int]) -> float:\n",
    "        n = len(a)\n",
    "        a.sort()\n",
    "        m =int(n/20)\n",
    "        b = a[m:-m]\n",
    "        ans = 0\n",
    "        for i in b:\n",
    "            ans +=i\n",
    "        return (ans/len(b))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        l = len(arr)\n",
    "        n = int(l*0.05)\n",
    "        arr1 = arr[n:l-n]\n",
    "        return sum(arr1) / len(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        nlen = len(arr)\n",
    "        min1 = max1 = int(nlen*0.05)\n",
    "        return sum(arr[min1:nlen-max1])/(nlen-max1-min1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr)\n",
    "        return sum(arr[n // 20 : -n // 20]) / (n*0.9)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def insertSort(self,nums:List[int]):\n",
    "        i=j=0\n",
    "        for i in range(1,len(nums)):\n",
    "            tmp  = nums[i]\n",
    "            for j in range(i-1,-2,-1):\n",
    "                if nums[j] > tmp:\n",
    "                    nums[j+1] = nums[j]\n",
    "                    # print(\"T\")\n",
    "                else: break\n",
    "            nums[j+1] = tmp\n",
    "            \n",
    "\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        self.insertSort(arr)\n",
    "        cnt = int(len(arr)/20)\n",
    "        sum =0.0\n",
    "        for item in arr[cnt:len(arr)-cnt]:\n",
    "            sum+=item\n",
    "        return sum/(len(arr)-2*cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        x = int(len(arr) * 0.05)\n",
    "        return sum(arr[x:-x]) / (len(arr)-2*x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        n = len(arr)\n",
    "        arr.sort()\n",
    "\n",
    "        return sum(arr[n//20:-n//20]) / (n*0.9)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr)\n",
    "        x = n * 5 // 100\n",
    "        total = 0\n",
    "        for i in range(x, n-x):\n",
    "            total += arr[i]\n",
    "        avg = total / (n - 2 * x)\n",
    "        return avg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: list[int]) -> float:\n",
    "        a = int(len(arr) / 20)\n",
    "        for i in range(a):\n",
    "            maxval = max(arr)\n",
    "            minval = min(arr)\n",
    "            arr.remove(maxval)\n",
    "            arr.remove(minval)\n",
    "        total = sum(arr)\n",
    "        return total / len(arr) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n=len(arr)\n",
    "        b=n//20\n",
    "        e=n-n//20\n",
    "        r=0\n",
    "        for i in range(b,e):\n",
    "            r+=arr[i]\n",
    "        return r/(n-n//10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n=len(arr)\n",
    "        tot=0\n",
    "        delta=int(n*0.05)\n",
    "        for i in range(delta,n-delta):\n",
    "            tot+=arr[i]\n",
    "        return tot*1.0/(n*0.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        n = len(arr)\n",
    "        delete = int(n * 0.05)\n",
    "        arr.sort()\n",
    "        total = arr[delete:-delete]\n",
    "        return sum(total)/(n-2*delete)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        l = len(arr) // 20\n",
    "        return sum(arr[l : len(arr) - l]) / (len(arr) - l * 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr)\n",
    "        m = int(n * 0.05)\n",
    "        res = 0\n",
    "        for i in range(m,n-m):\n",
    "            res += arr[i]\n",
    "        return res/(n-2*m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        # 排序数组\n",
    "        arr.sort()\n",
    "        \n",
    "        # 计算需要删除的元素数量\n",
    "        n = len(arr)\n",
    "        remove_count = int(n * 0.05)\n",
    "        \n",
    "        # 计算剩余数字的平均值\n",
    "        return sum(arr[remove_count: n - remove_count]) / (n - 2 * remove_count)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        n = len(arr)\n",
    "        arr = sorted(arr)\n",
    "        a = int(n*0.05)\n",
    "        arr = arr[a:-a]\n",
    "        sum = 0\n",
    "        for i in arr:\n",
    "            sum+=i\n",
    "        if n*0.9 == 0:\n",
    "            return 0\n",
    "        else:\n",
    "            a = sum/(n*0.9)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr) // 20\n",
    "        return mean(arr[n : n * 19])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort(reverse=True)\n",
    "        a=int(len(arr)*0.05)\n",
    "        arr=arr[a:len(arr)-a]\n",
    "        return sum(arr)/len(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "         arr.sort()\n",
    "         n = int(len(arr) * 0.05)\n",
    "         return sum(arr[n:n*(-1)])/(len(arr)-2*n)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        a = arr.__len__()\n",
    "        arr1 = arr[int(a*0.05):int(a*0.95)]\n",
    "        b = sum(arr1)/arr1.__len__()\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def InsertionSort(self,a):\n",
    "        n = len(a)\n",
    "        for i in range(1,n):\n",
    "            x = a[i]\n",
    "            j = i-1\n",
    "            while j >= 0:\n",
    "                if x >= a[j]:\n",
    "                    a[j+1] = a[j]\n",
    "                    j -= 1\n",
    "                else:\n",
    "                    break\n",
    "            a[j+1] = x\n",
    "\n",
    "\n",
    "\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        self.InsertionSort(arr)\n",
    "        n = len(arr)\n",
    "        cnt = n //20\n",
    "        ans = 0\n",
    "        for i in range(cnt,n-cnt):\n",
    "            ans += arr[i]\n",
    "        return ans /(n - 2*cnt)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        x = len(arr) // 20\n",
    "        for i in range(x):\n",
    "            arr.pop(0)\n",
    "            arr.pop(-1)\n",
    "        result = sum(arr) / len(arr)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr)\n",
    "        x = n * 5 // 100\n",
    "        total = sum(arr[x:n-x])\n",
    "        avg = total / (n - 2 * x)\n",
    "        return avg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr)\n",
    "        del_num = int(0.05*n)\n",
    "        return sum(arr[del_num: n-del_num])/(n-2*del_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr) * 0.05\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            arr.pop(0)\n",
    "            arr.pop(-1)\n",
    "            i += 1\n",
    "        return sum(arr)/len(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr=sorted(arr)\n",
    "        n=len(arr)\n",
    "        amount=sum(arr[n//20:19*n//20])/(0.9*n)\n",
    "        return amount"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        pfh_5 = int(len(arr) * 0.05)\n",
    "        new_l = arr[pfh_5:-pfh_5]\n",
    "        avg = sum(new_l) / len(new_l)\n",
    "        return avg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        \n",
    "        # 计算要删除的元素个数\n",
    "        remove_count = int(len(arr) * 0.05)\n",
    "        \n",
    "        # 删除最小的5%和最大的5%\n",
    "        arr = arr[remove_count:-remove_count]\n",
    "        \n",
    "        # 计算平均值\n",
    "        average = sum(arr) / len(arr)\n",
    "        \n",
    "        return average"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        length = len(arr)\n",
    "        arr = arr[int(length*0.05):-int(length*0.05)]\n",
    "        return sum(arr)/len(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        pfh_5 = int(len(arr) * 0.05)\n",
    "        new_l = arr[pfh_5:-pfh_5]\n",
    "        avg = sum(new_l) / len(new_l)\n",
    "        return avg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        length = len(arr)\n",
    "        arr = arr[int(length*0.05):-int(length*0.05)]\n",
    "        return sum(arr)/len(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        n = len(arr) \n",
    "        arr.sort()\n",
    "        return sum(arr[n // 20: -n // 20]) / (n * 0.9)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        l = len(arr)\n",
    "        n = int(l*0.05)\n",
    "        arr1 = arr[n:l-n]\n",
    "        return sum(arr1) / len(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        length = len(arr)\n",
    "        arr = arr[int(length*0.05):-int(length*0.05)]\n",
    "        return sum(arr)/len(arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        l = len(arr)\n",
    "        n = int(l*0.05)\n",
    "        # arr1 = arr[n:l-n]\n",
    "        return sum(arr[n:l-n]) / (l-2*n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        n = len(arr)\n",
    "        arr.sort()\n",
    "        return sum(arr[n // 20: -n //20]) / (n * 0.9)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        \n",
    "        # 计算要删除的元素个数\n",
    "        remove_count = int(len(arr) * 0.05)\n",
    "        \n",
    "        # 删除最小的5%和最大的5%\n",
    "        arr = arr[remove_count:-remove_count]\n",
    "        \n",
    "        # 计算平均值\n",
    "        average = sum(arr) / len(arr)\n",
    "        \n",
    "        return average"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr = sorted(arr)\n",
    "        n = len(arr)\n",
    "        return sum(arr[n //20 : 19*n//20]) / (0.9*n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        length = len(arr)\n",
    "        minNumber = int(length * 0.05)\n",
    "        maxNumber = int(length * 0.05)\n",
    "        tail = -1\n",
    "        for index in range(minNumber):\n",
    "            arr[index] = 0\n",
    "        for _ in range(maxNumber):\n",
    "            arr[tail] = 0\n",
    "            tail -= 1\n",
    "        return sum(arr) / (length - minNumber - maxNumber)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr)\n",
    "        min_5 = int(n * 0.05)\n",
    "        max_5 = int(n * 0.05)\n",
    "        return sum(arr[min_5:-max_5]) / (n-min_5-max_5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n=len(arr)\n",
    "        return sum(arr[n//20:n*19//20])/(0.9*n)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        #brr=sort(arr)\n",
    "        arr.sort()\n",
    "        l=len(arr)\n",
    "        a=int(l*0.05)\n",
    "        print(l,arr[a:-a])\n",
    "        uuu=sum(arr[a:-a])\n",
    "        print(uuu/(l-a*2))\n",
    "        return(uuu/(l-a*2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        return sum(arr[int(len(arr)*.05):int(len(arr)*.95)])/len(arr[int(len(arr)*.05):int(len(arr)*.95)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def trimMean(self, arr: List[int]) -> float:\r\n",
    "        n = len(arr)\r\n",
    "        arr.sort()\r\n",
    "        k = n // 20\r\n",
    "        return sum(arr[k:-k]) / (n - 2 * k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trimMean(self, arr: List[int]) -> float:\n",
    "        arr.sort()\n",
    "        n = len(arr)\n",
    "        return sum(arr[n//20: n - n//20])/(n - n//10)\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
